package fun.werfamily.wf.base.vo;


import fun.werfamily.wf.base.enums.CommonResponseEnum;
import fun.werfamily.wf.base.exception.CommonError;

import java.io.Serializable;

/**
 * @Author Mr.WenMing
 * @date 2022/8/20 14:52
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -8782333365744933352L;
    protected String code = CommonResponseEnum.OK.getCode();
    protected String msg;
    protected boolean success = true;
    protected T data;
    protected String traceId;

    protected Result() {
    }

    protected Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    protected Result(String code, T data, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> error(String traceId) {
        return error(CommonResponseEnum.SERVER_ERROR.getCode(), CommonResponseEnum.SERVER_ERROR.getFriendlyMsg(), traceId);
    }

    public static <T> Result<T> error(CommonError commonError) {
        return error(commonError.getCode(), commonError.getFriendlyMsg());
    }

    public static <T> Result<T> error(String msg, String traceId) {
        return error(CommonResponseEnum.SERVER_ERROR.getCode(), msg, traceId);
    }

    public static <T> Result<T> error(String code, String msg, String traceId) {
        Result<T> result = new Result(code, msg);
        result.setSuccess(false);
        result.setTraceId(traceId);
        return result;
    }

    public static <T> Result<T> error(String code, T data, String msg, String traceId) {
        Result<T> result = new Result(code, data, msg);
        result.setSuccess(false);
        result.setTraceId(traceId);
        return result;
    }

    public static <T> Result<T> success() {
        return success(CommonResponseEnum.OK.getCode(), CommonResponseEnum.OK.getFriendlyMsg());
    }

    public static <T> Result<T> success(T data) {
        return success(data, CommonResponseEnum.OK.getFriendlyMsg());
    }

    public static <T> Result<T> success(String msg) {
        return success(null, msg);
    }

    public static <T> Result<T> success(T data, String msg) {
        return new Result("200", data, msg);
    }

    public static <T> Result<T> success(String code, String msg) {
        return new Result(code, msg);
    }

    public String getcode() {
        return this.code;
    }

    public void setcode(String code) {
        this.code = code;
    }

    public String getmsg() {
        return this.msg;
    }

    public void setmsg(String msg) {
        this.msg = msg;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result<T> data(T data) {
        return success(data, (String) null);
    }

    public String getTraceId() {
        return this.traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
